package com.spider.gp.processor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spider.gp.constant.UrlConstant;
import com.spider.gp.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.math.BigDecimal;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * description:
 *
 * @author clare
 * @date 2022/05/20 08:56
 **/
@Component
public class GpHyProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Autowired
    private GpHyDao gpHyDao;
    @Autowired
    private HyDao hyDao;

    // f2 当前价格 f9 动态市盈率 f12 股票编码 f14股票名称
    @Override
    public void process(Page page) {
        List<String> all = page.getJson().jsonPath("$.data.diff").all();
        String s = page.getUrl().toString().split("BK")[1];
        LambdaQueryWrapper<Hy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Hy::getCode , "BK"+s);
        Hy hy = hyDao.selectOne(wrapper);
        List<GpHy> gpHyList = new ArrayList<>();
        String nameC = hy.getName();
        all.forEach(item ->   {
            HashMap hashMap = JSON.parseObject(item, HashMap.class);
            String price  = hashMap.get("f2").toString();
            String dtsyl = hashMap.get("f9").toString();
            String code = hashMap.get("f12").toString();
            String name = hashMap.get("f14").toString();
            GpHy build = GpHy.builder().hy(nameC).price(price).code(code).name(name).dtsyl(dtsyl).build();
            gpHyList.add(build);
        });
        //后面测试下批量
        gpHyDao.insertBatchSomeColumn(gpHyList);

    }

    @Override
    public Site getSite() {
        return site;
    }
}








